﻿using Microsoft.EntityFrameworkCore;

namespace IS_Ever_Green.DataAccess.Model.Repositories
{
    public class UserRepository : IUserRepository
    {
        public UserRepository()
        {

        }

        public void RegisterUser(UserModel user)
        {
            using (var context = new EverGreenDBContext())
            { 
                context.Users.Add(user);
                context.SaveChanges();
            }
        }

        public bool UserExists(string login)
        {
            using (var context = new EverGreenDBContext())
            {
                return context.Users.Any(u => u.LoginUser == login);
            }
        }

        public UserModel AuthenticateUser(string login, string password)
        {
            using (var context = new EverGreenDBContext())
            {
                var user = context.Users
                    .Include(u => u.Role)
                    .FirstOrDefault(u => u.LoginUser == login);

                if (user != null && VerifyPassword(password, user.Password))
                {
                    return user;
                }

                return null;
            }
        }

        private bool VerifyPassword(string enteredPassword, string storedPassword)
        {
            // Сделать сравнение через BCrypt для хеша пароля

            return enteredPassword == storedPassword;
        }

        public string GetUserRole(UserModel user)
        {
            if (user != null && user.Role != null)
            {
                return user.Role.RoleName;
            }
            else
            {
                return "Unknown";
            }
        }

        public List<UserModel> GetEmployeesWithRole(string roleName)
        {
            using (var context = new EverGreenDBContext())
            {
                return context.Users
                               .Where(u => u.Role.RoleName == roleName)
                               .ToList();
            }
        }

        public void SaveUserProfile(UserModel user, byte[] photo)
        {
            using (var context = new EverGreenDBContext())
            {
                var userProfile = context.Users.FirstOrDefault(u => u.UserId == user.UserId);

                if(userProfile != null)
                {
                    userProfile.LoginUser = user.LoginUser;
                    userProfile.FirstName = user.FirstName;
                    userProfile.LastName = user.LastName;
                    userProfile.Email = user.Email;
                    userProfile.NumberPhone = user.NumberPhone;
                    userProfile.Password = user.Password;
                    userProfile.DeliveryAddress = user.DeliveryAddress;
                    userProfile.PhotoUser = photo;
                    context.SaveChanges();
                }
            }
        }

        public void EditUser(UserModel user)
        {
            using(var context = new EverGreenDBContext())
            {
                context.Users.Update(user);
                context.SaveChanges();
            }
        }

        public bool RecoveryPassword(string login, string email,string password)
        {
            using (var context = new EverGreenDBContext())
            {

                var user = context.Users.FirstOrDefault(u => u.LoginUser == login && u.Email == email);

                if (user != null)
                {
                    user.Password = password;

                    context.SaveChanges();

                    return true;
                }

                return false;
            }
        }

        public void DeleteUser(UserModel user)
        {
            using(var context = new EverGreenDBContext())
            {
                context.Users.Remove(user);
                context.SaveChanges();
            }
        }
    }
}
